package com.billdimmick.merkabah;

import java.util.Collection;

import junit.framework.Assert;

import org.junit.Test;

public class TestSQSManagerBase {
	
	protected SQSManager manager;
	
	@Test
	public void testListQueues() throws SQSException {
		final String [] names={"michelle", "stan"};
		for (String name: names) {
			manager.createQueue(name);
		}
		Collection<String> queues=manager.listQueues();
		for (String name: names) {
			Assert.assertTrue(queues.contains(name));
		}
	}
	
	@Test
	public void testListQueuesWithPrefix() throws SQSException{
		final String [] names={"michelle", "stan"};
		final String prefix = "st";
		for (String name: names) {
			manager.createQueue(name);
		}
		Collection<String> queues=manager.listQueues(prefix);
		for (String name: names) {
			if (name.startsWith(prefix)) {
				Assert.assertTrue(queues.contains(name));
			} else {
				Assert.assertFalse(queues.contains(name));				
			}
		}
	}

	@Test
	public void testCreateQueueSuccess() throws SQSException {
		final String name="bauhaus";
		final int count=manager.listQueues().size();
		manager.createQueue(name);
		Assert.assertEquals(count+1, manager.listQueues().size());
		manager.deleteQueue(name);
	}

	@Test
	public void testDeleteQueueSuccess() throws SQSException {
		final String name="stumptown";
		manager.createQueue(name);
		Collection<String> baseline=manager.listQueues();
		manager.deleteQueue(name);
		Collection<String> postdelete=manager.listQueues();
		Assert.assertEquals(baseline.size()-1, postdelete.size());
	}
	
	
	@Test
	public void testMessageRoundtrip() throws SQSException {
		final String queue="yo";
		final String body="Collins Pub Happy Hour";
		manager.createQueue(queue);
		manager.send(queue, body);
		Assert.assertEquals(1, manager.getApproximateNumberOfMessages(queue));
		Message m=manager.receive(queue);
		Assert.assertNotNull(m);
		Assert.assertNotNull(m.getMessageId());
		Assert.assertNotNull(m.getReceiptHandle());
		Assert.assertEquals(queue, m.getQueue());
		Assert.assertEquals(body, m.getMessage());
		try {
			m.setMessageId("badidea");
			Assert.fail("Should not be able to set message id on messages returned from SQS.");
		} catch (IllegalStateException e) {}
		manager.delete(m);
		m=manager.receive(queue);
		Assert.assertNull(m);
	}
}
